package com.sinosoft.common.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * 时间工具类
 * 
 * @author
 */
public class CardUtils extends org.apache.commons.lang3.time.DateUtils
{
    /**
     * 工具方法 —— 根据身份证号判断性别
     *
     * @param cardNo 身份证号
     * @return 性别
     */
    public static String getSex(String cardNo) {
        String sexStr = "0";
        if (cardNo.length() == 15) {
            sexStr = cardNo.substring(14, 15);
        } else if (cardNo.length() == 18) {
            sexStr = cardNo.substring(16, 17);
        }
        int sexNo = Integer.parseInt(sexStr);
        return sexNo % 2 == 0 ? "女" : "男";
    }

    /**
     * 工具方法 —— 根据身份证号判断年龄
     *
     * @param cardNo 身份证号
     * @return 年龄
     */
    public static int getAge(String cardNo) {
        if (cardNo == null || cardNo.length() != 18) {
            throw new IllegalArgumentException("身份证号码不正确");
        }

        String birthdayStr = cardNo.substring(6, 14);
        LocalDate birthday = LocalDate.parse(birthdayStr, DateTimeFormatter.ofPattern("yyyyMMdd"));
        LocalDate today = LocalDate.now();

        return Period.between(birthday, today).getYears();
    }
}
